package com.egg.mall.rabbitmq.listener;

import com.egg.basic.constant.BasicRabbitExchange;
import com.egg.basic.enums.pay.BusinessTypeEnum;
import com.egg.common.core.constant.StringPool;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.JsonUtil;
import com.egg.mall.constant.MallConstant;
import com.egg.mall.constant.MallRabbitExchange;
import com.egg.basic.model.rabbit.PayRabbitSender;
import com.egg.mall.model.rabbit.SkuOrderTimeoutRabbitSender;
import com.egg.mall.service.sku.order.ISkuOrdersService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * 监听
 *
 * @author east
 */
@Slf4j
@Async
@Component
public class SkuOrderListener {


    @Autowired
    private ISkuOrdersService skuOrdersService;


    /**
     * 监听-订单取消
     * 本队列 = 本系统 + 生产者渠道 + 生产者KEY
     * queue: mall.mall.exchange.x-delayed-message.order.cancel
     * exchange: mall.exchange.x-delayed-message.order
     * key : cancel
     *
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = MallConstant.SYSTEM_NAME + StringPool.DOT + MallRabbitExchange.ORDER_TIMEOUT_EXCHANGE + StringPool.DOT + RabbitRoutingKey.CANCEL)
                    , exchange = @Exchange(name = MallRabbitExchange.ORDER_TIMEOUT_EXCHANGE, type = "x-delayed-message")
                    , key = {RabbitRoutingKey.CANCEL}
            )
    })
    public void orderTimeout(Message message, Channel channel) {
        SkuOrderTimeoutRabbitSender param = JsonUtil.objToObj(message.getBody(), SkuOrderTimeoutRabbitSender.class);
        skuOrdersService.timeout(param.getOrderId());
    }

    /**
     * 监听-支付成功
     * 本队列 = 本系统 + 生产者渠道 + 生产者KEY
     *
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = MallConstant.SYSTEM_NAME + StringPool.DOT + BasicRabbitExchange.PAY_EXCHANGE + StringPool.DOT + RabbitRoutingKey.SUCCESS)
                    , exchange = @Exchange(name = BasicRabbitExchange.PAY_EXCHANGE)
                    , key = {RabbitRoutingKey.SUCCESS}
            )
    })
    public void orderPaySuccess(Message message, Channel channel) {
        PayRabbitSender param = JsonUtil.objToObj(message.getBody(), PayRabbitSender.class);
        if (!Objects.equals(param.getBusinessType(), BusinessTypeEnum.MALL_SKU_ORDER.getCode())) {
            return;
        }
        skuOrdersService.paySuccess(param.getBusinessId());
    }

}
